# -*- coding=utf-8 -*-
# @Time    : 2021/3/4 13:20
# @Author  : lhys
# @FileName: tools.py

import tkinter
    import tkinter.messagebox
import re

def calculate(entry):
    try:
        result = entry.get()
        if result == '':
            return
        result = eval(result)
        print(result)
        entry.delete(0, "end")
        entry.insert(0, str(result))
    except:
        tkinter.messagebox.showerror("错误", "格式错误!\n请重新输入！")

class myButton():
    def __init__(self, frame, text, entry, **kwargs):
        side = kwargs.get('side') if 'side' in kwargs else ()
        self.btn = tkinter.Button(
            frame,
            text = text,
            activeforeground="blue",
            activebackground="pink",
            width="13",
            command=lambda :entry.insert("end", text)
        )
        if side:
            self.btn.grid(row=side[0], column=side[1])
        else:
            self.btn.pack()

def o2h(entry):
    num = entry.get()
    if re.findall('\D', num):
        tkinter.messagebox.showerror("错误", "格式错误!\n请重新输入！")
        return
    entry.delete(0, 'end')
    entry.insert(0, hex(eval(num)))
    print('Hex successfully!')